'''
Created on 07/06/2010

@author: Will
'''
from pyglet.sprite import Sprite
from utils import ImageCache
import math
import pymunk as pm

from pymunk import Vec2d

 

class RealObject(Sprite):


    def get_leftTop(self):
        x = self.x + (-self.image.width/2*self.scale)*math.cos(self.rotation) + (-self.image.width/2*self.scale)*math.sin(self.rotation)
        y = self.y - (self.image.height/2*self.scale)*math.sin(self.rotation) + (self.image.height/2*self.scale)*math.cos(self.rotation)
        
        return x, y 
    
    def destroy(self):
        self._set_batch(None)

    def get_rightTop(self):
        x = self.x + (self.image.width/2*self.scale)*math.cos(self.rotation) + (self.image.width/2*self.scale) *math.sin(self.rotation)
        y = self.y -(self.image.height/2*self.scale)*math.sin(self.rotation) + (self.image.height/2*self.scale)*math.cos(self.rotation)
        return x,y
   
    def get_leftBottom(self):
        x = self.x + (-self.image.width/2*self.scale)*math.cos(self.rotation)  + (-self.image.width/2*self.scale) *math.sin(self.rotation)
        y = self.y - (-self.image.height/2*self.scale)*math.sin(self.rotation) + (-self.image.height/2*self.scale)*math.cos(self.rotation)
        return x, y
  
    def get_rightBottom(self):
        x = self.x + (self.image.width/2*self.scale) * math.cos(self.rotation) +( self.image.width/2 *self.scale)*math.sin(self.rotation)
        y = self.y - (-self.image.height/2*self.scale)*math.sin(self.rotation) +(-self.image.height/2*self.scale)*math.cos(self.rotation)
        return x,y
    #bottom = property(__get_bottom)
    
    def __init__(self,x, y, rot,imageFile = "box.png",batch = None,mass1 = None):

        self.imagem = ImageCache.getImage(imageFile)
        self.imagem.anchor_x = self.imagem.width/2
        self.imagem.anchor_y = self.imagem.height/2
        super(RealObject,self).__init__(self.imagem, 0,0,batch=batch)
        self.id = id
        self.scale = 1
        
        self.x = x
        self.y = y
        self.childs = []
        self.dead = False
        self.isCar = False
        if mass1 != None:
            self.mass = mass1
        else:
            self.mass = 0.01 * self.image.width * self.image.height
        inertia = pm.moment_for_box(self.mass, self.image.width, self.image.height)
        self.body = pm.Body(self.mass, inertia)
        self.body.position =( self.x, self.y)
        self.shape = pm.Poly(self.body, self.getRelPoly(),(0,0),True)
        self.rotation = rot
        self.body._set_angle(rot)
        self.shape.friction = 0.5
        self._set_rotation(rot)
    def getRelHeight(self):
        return self.imagem.height/2
    
    def getRelWidth(self):
        return self.imagem.width/2
    
    
    def getRelPoly(self):
        return [(-self.getRelWidth(), self.getRelHeight()),
                (-self.getRelWidth(),-self.getRelHeight()),
                ( self.getRelWidth(),-self.getRelHeight()),
                ( self.getRelWidth(), self.getRelHeight())]
        
    def physicsUpdate(self):
                
        velocity = self.body._get_velocity()
        force = Vec2d(-velocity[0]*self.body.mass*0.1,-velocity[1]*self.body.mass*0.1)        
        self.body.apply_impulse(force, (0,0))
        
        self.body._set_torque(-self.body._get_angular_velocity()*self.body.mass*1500)

        self.x = self.body.position.x
        self.y = self.body.position.y
        
        self.rotation = math.degrees(self.body._get_angle())
        if self.rotation <= math.pi:
            self.rotation = math.pi - self.rotation
        else:
            self.rotation = 2*math.pi - self.rotation
            
    def getSpeed(self):
        return self.body._get_velocity().get_length()
            
    def tick(self,keyboard):
        self.physicsUpdate()
        
        for i in self.childs:
            i.tick()
    
    def getPosition(self):
        return self.x,self.y
    
    def setPosition(self,pos):
        self.body.position.x = pos[0]
        self.body.position.y = pos[1]
    
    def getRotatedTopPoint(self):
        point = Vec2d(0,self.getRelHeight())
        point = point.rotated(self.body._get_angle())
        return point.x,point.y
    
    
    def getRotatedPoint(self,point):
        point = Vec2d(point)
        point = point.rotated(self.body._get_angle())
        return point
        
    
    def isAt(self,x,y):
        poly = (self.get_leftBottom(), self.get_leftTop(),self.get_rightTop(),self.get_rightBottom())
        n = len(poly)
        inside =False
    
        p1x,p1y = poly[0]
        for i in range(n+1):
            p2x,p2y = poly[i % n]
            if y > min(p1y,p2y):
                if y <= max(p1y,p2y):
                    if x <= max(p1x,p2x):
                        if p1y != p2y:
                            xinters = (y-p1y)*(p2x-p1x)/(p2y-p1y)+p1x
                        if p1x == p2x or x <= xinters:
                            inside = not inside
            p1x,p1y = p2x,p2y
        return inside

    def on_click(self):
        print self, self.mass, self.image.height,self.image.width

    